<template>
  <!-- 顶部按钮 -->
  <div class="topButton">
    <div class="btnItem">
      <div class="top" v-for="(item, idx) in topBtnList" :key="idx" :style="{ color: className.btnColor }">
        <i :class="item.icon"></i>
        <span>{{ item.btnName }}</span>
      </div>
    </div>
    <div :style="{ color: className.dateTimeColor }">{{ dateTime }}</div>
  </div>
</template>

<script>
import { dateFormat } from '@/commont/utils/timeUtil.js'
export default {
  components: {},
  props: {
    topBtnList: {
      type: Array,
      default: () => []
    },
    className: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      dateTime: '',
      newTimer: ''//定时器
    }
  },
  computed: {},
  watch: {},
  created() { },
  mounted() {
    this.getDateTime();
    clearInterval(this.newTimer); //清除定时器
    // 定时获取时间
    this.newTimer = setInterval(this.getDateTime, 1000);
  },
  updated() { },
  methods: {
    // 获取系统时间
    getDateTime() {
      let date = new Date();
      this.dateTime = dateFormat(date, "MM月DD日 HH:mm");
    }
  },
  filters: {},
  beforeRouteLeave(to, from, next) {
    // 离开组件时停掉定时器，节省内存
    clearInterval(this.newTimer);
    next();
  },
}
</script>

<style lang="scss" scoped>
.topButton {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .btnItem {
    display: flex;
    flex-direction: row;

    .top {
      margin: 0 10px;
    }
  }
}
</style>